package com.victor.controller.common;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.comments.Comments;
import com.victor.entity.praise.CommentsPraise;
import com.victor.entity.toreport.ToReport;
import com.victor.service.CommentsService;
import com.victor.utils.ClassFilter;
import com.victor.utils.GetPage;
import com.victor.utils.Msg;

@RestController
@RequestMapping("/comments")
public class CommentsController {
	@Autowired
	private CommentsService service;
	/**
	 * 查询评论
	 * @param type
	 * @param pid
	 * @param uid
	 * @param page
	 * @param row
	 * @param flag
	 * @return
	 */
	@RequestMapping("/findcomments")
	public Msg findComments(String type,String pid,String uid,int page,int row,String flag) {
		if (type == null || type.isEmpty() || pid == null || pid.isEmpty() || flag == null ||flag.isEmpty()) {
			return Msg.fail("入参失败");
		}
		List<Comments> comments = service.selCommentsByTypeAndPid(type,pid,uid,flag);
		GetPage<Comments> pageInfo = null;
			if (comments != null && comments.size()>0) {
				for (int i = 0; i < comments.size(); i++) {
					List<Comments> list = service.selCommentsByFid(comments.get(i).getId(),uid);
					comments.get(i).setComments(list);
					if (list != null && list.size()>0) {
						for (int j = 0; j < list.size(); j++) {
							List<Comments> selCommentsByFid = service.selCommentsByFid(list.get(j).getId(), uid);
							list.get(j).setComments(selCommentsByFid);
							
						}
					}
				}
				 pageInfo = GetPage.pageInfo(page, row, comments);
			}
		return Msg.success().add("list", pageInfo);
		
	}
	
	/**
	 * 点赞评论
	 * @param commentid
	 * @param uid
	 * @return
	 */
	@RequestMapping("/praisecomments")
	public Msg commentsPraise(String commentid,String uid) {
		if (commentid == null || commentid.isEmpty() || uid== null || uid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		CommentsPraise commentsPraise = new CommentsPraise();
		commentsPraise.setCid(commentid);
		commentsPraise.setUid(uid);
		CommentsPraise selCommentsPriaeByCid = service.selCommentsPriaeByCidAndUid(commentid,uid);
		if (selCommentsPriaeByCid == null ) {
			boolean insertCommentsPraise = service.insertCommentsPraise(commentsPraise);
			if (!insertCommentsPraise) {
				return Msg.fail("添加评论点赞失败");
			}
			//点赞数
			String praisecount = service.selCommentsPraiseByCid(commentid);
			return Msg.success().add("return", "点赞成功").add("praisecount", praisecount);
		}else {
			boolean delCommentsPraise = service.delCommentsPraise(selCommentsPriaeByCid.getId());
			if (!delCommentsPraise) {
				return Msg.fail("删除评论点赞失败");
			}
			//点赞数
			String praisecount = service.selCommentsPraiseByCid(commentid);
			return Msg.success().add("return", "取消点赞成功").add("praisecount", praisecount);
		}
		
	}
	
	/**
	 * 发布评论
	 * @param comments
	 * @return
	 */
	@RequestMapping("/addcomment")
	public Msg commentsCity(@RequestBody Comments comments) {
		String[] str = {"uid","content","pid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(comments, str);
		if (empty== true) {
			return Msg.fail("入参错误");
		}
		boolean insertComments = service.insertComments(comments);
		if (!insertComments) {
			return Msg.fail("添加评论错误");
		}
			if (comments.getCommentPhotos() != null && comments.getCommentPhotos().size()>0) {
				for (int i = 0; i < comments.getCommentPhotos().size(); i++) {
					comments.getCommentPhotos().get(i).setCid(String .valueOf(comments.getId()));
					boolean insertCommentsPhoto = service.insertCommentsPhoto(comments.getCommentPhotos().get(i));
					if (!insertCommentsPhoto) {
						return Msg.fail("添加评论图片失败");
					}
					
				}
		}
		
		return Msg.success();
	}
	/**
	 * 举报
	 * @param toReport
	 * @return
	 */
	@RequestMapping("/toreport")
	public Msg addToReport(@RequestBody ToReport toReport) {
		String[] str = {"fromuid","touid","reson","type","cid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(toReport, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		boolean b = service.insertToReport(toReport);
		if (toReport.getPhotos() != null && toReport.getPhotos().size()>0) {
			for (int i = 0; i < toReport.getPhotos().size(); i++) {
				toReport.getPhotos().get(i).setTid(String.valueOf(toReport.getId()));
				boolean insertToReportPhoto = service.insertToReportPhoto(toReport.getPhotos().get(i));
				if (!insertToReportPhoto) {
					return Msg.fail("添加图片错误");
				}
			}
		}
		return Msg.judge(b);
		
	}
}
